import {createApp} from 'vue'
import App from './App.vue'

// Vuetify
import 'vuetify/styles' //'vuetify/styles' should not be used in sass files as it resolves to precompiled css
import {createVuetify} from 'vuetify'
import { pl, zhHans } from 'vuetify/locale'
import * as directives from 'vuetify/directives'
import '@mdi/font/css/materialdesignicons.css'
import * as components from 'vuetify/components'
import * as labsComponents from 'vuetify/labs/components'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'

// Router
import {createRouter, createWebHashHistory} from 'vue-router'
import Login from "./Login.vue";
import Backend from "./Backend.vue";
import Users from "./Users.vue";
import CheckItem from "./CheckItem.vue";
import Role from "./role.vue";
import Dept from "./dept.vue";
import Menu from "./menu.vue";
import FunctionOffice from "./functionOffice.vue";
import CheckPlace from "./checkPlace.vue";
import CheckRecord from "./checkRecord.vue";
import CheckFire from "./fireRecord.vue";
import CheckRecordAbnormal from "./checkRecordAbnormal.vue";
import CheckFireAbnormal from "./fireRecordAbnormal.vue";
import Building from "./building.vue";
import Floor from "./floor.vue";
import Hydrant from "./hydrant.vue";
import FireItem from "./fireItem.vue";
import CheckNo from "./fireRecordNo.vue";
import DayRecord from "./dayRecord.vue";
import MonthRecord from "./monthRecord.vue";
import MonthFireRecord from "./monthFireRecord.vue";
import GriddingPlace from "./griddingPlace.vue";
import Mobile from "./Mobile.vue";
import Cockpit from "./Cockpit.vue";
import Visit from "./visit.vue";
import Behavior from "./behavior.vue"
import Smog from "./smog.vue"
import Intrusion from "./intrusion.vue"
import Opinion from "./opinion.vue"
import BuliangData from "./buildingData.vue"
import SysDict from "./sysDict.vue"
import Camera from "./camera.vue"
import Doors from "./doors.vue"
import Info from "./info.vue"
import WindowPlace from "./windowPlace.vue"
import WindowList from "./windowList.vue"
import NotFound from "./notFound.vue"
import Home from "./home.vue"

// Axios
import axios from 'axios'
import {snackbar} from "./store";

const vuetify = createVuetify({
    directives,
    components: {
        ...components,
        ...labsComponents,

    },
    locale: {
        locale: 'zhHans'
      },
    theme: {
        themes: {
            light: {
                dark: false,
                colors: {
                    primary: '#0071EB',
                    background: '#f5f5f7'
                }
            },
        },
    }
})

export const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        {
            path: '/backend',
            component: Backend,
            redirect: '/users',
            children: [
                {path: '/users', component: Users},
                {path: '/checkItem', component: CheckItem},
                {path: '/role', component: Role},
                {path: '/dept', component: Dept},
                {path: '/menu', component: Menu},
                {path: '/functionOffice', component: FunctionOffice},
                {path: '/checkPlace', component: CheckPlace},
                {path: '/checkRecord', component: CheckRecord},
                {path: '/checkRecordAbnormal', component: CheckRecordAbnormal},
                {path: '/checkFire', component: CheckFire},
                {path: '/checkFireAbnormal', component: CheckFireAbnormal},
                {path: '/building', component: Building},
                {path: '/floor', component: Floor},
                {path: '/hydrant', component: Hydrant},
                {path: '/fireItem', component: FireItem},
                {path: '/checkNo', component: CheckNo},
                {path: '/dayRecord', component: DayRecord},
                {path: '/monthRecord', component: MonthRecord},
                {path: '/monthFireRecord', component: MonthFireRecord },
                {path: '/grid', component: GriddingPlace},
                {path: '/visit', component: Visit},
                {path: '/behavior', component: Behavior},
                {path: '/smog', component: Smog},
                {path: '/intrusion', component: Intrusion},
                {path: '/opinion', component: Opinion},
                {path: '/buliangData', component: BuliangData},
                {path: '/sysDict', component: SysDict},
                {path: '/camera', component: Camera},
                {path: '/doors', component: Doors},
                {path: '/info', component: Info},
                {path: '/windowPlace', component: WindowPlace},
                {path: '/windowList', component: WindowList},
                {path: '/home', component: Home}
            ]
        },
        {
            path: '/mobile',
            component: Mobile
        },
        {
            path: '/cockpit',
            component: Cockpit
        },
        {path: '/login', component: Login},
        {path: '/:pathMatch(.*)*', component: NotFound}
    ]
})
// Axios
// 第一医院224
// axios.defaults.baseURL = 'http://192.192.1.224:8092/smartHospital';
// 第二医院21
// axios.defaults.baseURL = 'http://10.10.10.49:8092/smartHospital';
// axios.defaults.baseURL = 'http://10.10.10.49:9991/healthCommission';
axios.defaults.baseURL = 'http://101.68.222.195:9991/healthCommission';
// axios.defaults.baseURL = 'http://10.10.10.49:8092/smartHospital';
// axios.defaults.baseURL = 'https://api.hdingding.jxtvtech.com/smartHospital/';
axios.defaults.headers.post['Content-Type'] = 'application/json';
axios.defaults.headers.put['Content-Type'] = 'application/json';

axios.interceptors.request.use(config => {
    const token = localStorage.getItem('token');
    if (token) {
        config.headers['Authorization'] = 'Bearer ' + token;
    }
    return config;
})

axios.interceptors.response.use(res => {
    switch (res.data.code) {
        case 200:
            return res;
        case 401:
            snackbar.info('认证失败,请重新登录')
            localStorage.removeItem('token')
            router.push('/login')
        break
        default:
            snackbar.info(res.data.message)
            return Promise.reject(res);
    }
}, err => {
    if (err.data) {
        switch (err.data.status) {
            case 401:
                snackbar.info('认证失败,请重新登录')
                localStorage.removeItem('token')
                router.push('/login')
                break
            case 403:
                snackbar.info('无权限')
                break
            case 500:
                snackbar.info(err.data.message)
                break
            default:
                snackbar.info(err.data.status + err.data.message)
        }
    }
    return Promise.reject(err);
})

createApp(App).use(ElementPlus).use(router).use(vuetify).mount('#app')

